//
//  SetUpTableViewController.m
//  17DianYing
//
//  Created by apple on 15/9/15.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "SetUpTableViewController.h"
#import "AFNetworking.h"

@interface SetUpTableViewController ()
@property (weak, nonatomic) IBOutlet UILabel *setUpLabel;

@end

@implementation SetUpTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
    
    NSLog(@"files :%lu",(unsigned long)[files count]);
    NSString *a = [NSString stringWithFormat:@"%luM",[files count]];
    //    NSLog(@"%@",a);
    
    self.setUpLabel.text = a;
    
    for (NSString *p in files) {
        NSError *error;
        NSString *path = [cachPath stringByAppendingPathComponent:p];
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
            [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
        }
    }
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        
        [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];
        
        
    } else if(indexPath.section == 1 && indexPath.row == 1){
        NSString * appstoreUrlString = @"http://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?mt=8&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&type=Purple+Software&id=APP_ID";
        
        NSURL * url = [NSURL URLWithString:appstoreUrlString];
        
        if ([[UIApplication sharedApplication] canOpenURL:url])
        {
            [[UIApplication sharedApplication] openURL:url];
        }
        else
        {
            NSLog(@"can not open");
        }

        
    }else if (indexPath.section == 1 && indexPath.row == 3){
        
        NSString * url =[http stringByAppendingString:@"/mobile/user/center/logout"];
        
        NSUserDefaults *to = [NSUserDefaults standardUserDefaults];
        NSString *token = [to objectForKey:@"usertoken"];
        
        NSLog(@"%@",token);
        NSDictionary * dic =  @{@"token":token};
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        
        [manager POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSDictionary * d=operation.responseObject;
            NSDictionary *dic = d;
            
            NSLog(@"-----------------%@",dic);
            NSString *str = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            if ([str isEqualToString:@"ok"]) {

                
                UIStoryboard *s = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
                UIViewController *vc = [s instantiateViewControllerWithIdentifier:@"Enteruser"];
                [self presentViewController:vc animated:YES completion:nil];
                
                //删除数据
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"usertoken"];
            }
 
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"_________________________%@",error.localizedDescription);
        }];


        
        //        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

-(void)clearCacheSuccess
{
    NSLog(@"清理成功");
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"数据" message:@"清除成功" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alter show];
    self.setUpLabel.text = @"0M";
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}






/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
